function legendBox=prettyLegend(ax)
if nargin<1
    ax=gca;
end
hold on
legendTitleColor=[0,0,0];
if mean(ax.Color)<0.5
    legendTitleColor=[1,1,1];
    ax.Legend.TextColor=[1,1,1];
end

ax.Legend.AutoUpdate='on';
% ax.Legend.FontSize=11;
% ax.Legend.Title.FontSize=14;
ax.Legend.AutoUpdate='off';

% 如果在图窗外则不设框
if ~isempty(regexpi(ax.Legend.Location,'out', 'once'))
    ax.Legend.Box='off';
    lgdPos=ax.Legend.Position;
    % 依据legend所处figure坐标和axes范围计算Legend坐标
    xyMin=[(lgdPos(1)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...
        (lgdPos(2)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];
    xyMax=[(lgdPos(1)+lgdPos(3)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...
        (lgdPos(2)+lgdPos(4)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];
    ax.Legend.UserData.NewBkg=[];
    % 隐藏原标题
    ax.Legend.Title.Visible='off';
    % 绘制新legend标题
    ax.Legend.UserData.NewTitle=text(ax,xyMin(1),xyMax(2),['  ',ax.Legend.Title.String],...
        'FontSize',ax.Legend.Title.FontSize,'VerticalAlignment','top','FontWeight','bold','Color',legendTitleColor);
else

    ax.Legend.Box='off';
    lgdPos=ax.Legend.Position;
    % 依据legend所处figure坐标和axes范围计算Legend坐标
    xyMin=[(lgdPos(1)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...
        (lgdPos(2)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];
    xyMax=[(lgdPos(1)+lgdPos(3)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...
        (lgdPos(2)+lgdPos(4)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];
    xDiff=(xyMax(1)-xyMin(1));
    yDiff=(xyMax(2)-xyMin(2));
    % 绘制圆角矩形作为新框
    ax.Legend.UserData.NewBkg=rectangle(ax,'Position',[xyMin,xDiff,yDiff],'Curvature',0.2,...
        'LineWidth',1.2,'EdgeColor',[0.39 0.41 0.39],'FaceColor',[1 1 1 .2]);
    %ax.Legend.Title.FontSize=14;
    % 隐藏原标题
    ax.Legend.Title.Visible='off';
    % 绘制新legend标题
    ax.Legend.UserData.NewTitle=text(ax,xyMin(1),xyMax(2),['  ',ax.Legend.Title.String],...
        'FontSize',ax.Legend.Title.FontSize,'VerticalAlignment','top','FontWeight','bold','Color',legendTitleColor);
end

% 返回值
legendBox.Title=ax.Legend.UserData.NewTitle;
legendBox.Box=ax.Legend.UserData.NewBkg;

oriFunc=ax.Parent.WindowButtonMotionFcn;
set(ax.Parent,'WindowButtonMotionFcn',@bt_move);% 设置鼠标移动回调
    function bt_move(~,~)
        oriFunc();
        if ~isempty(regexpi(ax.Legend.Location,'out', 'once'))
            lgdPos=ax.Legend.Position;
            % 依据legend所处figure坐标和axes范围计算Legend坐标
            xyMin=[(lgdPos(1)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...
                (lgdPos(2)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];
            xyMax=[(lgdPos(1)+lgdPos(3)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...
                (lgdPos(2)+lgdPos(4)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];
            xyMin(1)=max(xyMin(1),ax.XLim(1));
            xyMin(2)=max(xyMin(2),ax.YLim(1));
            xyMax(1)=min(xyMax(1),ax.XLim(2));
            xyMax(2)=min(xyMax(2),ax.YLim(2));
            % 重设位置属性
            ax.Legend.UserData.NewTitle.Position=[xyMin(1),xyMax(2)];
        else
            lgdPos=ax.Legend.Position;
            % 依据legend所处figure坐标和axes范围计算Legend坐标
            xyMin=[(lgdPos(1)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...
                (lgdPos(2)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];
            xyMax=[(lgdPos(1)+lgdPos(3)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...
                (lgdPos(2)+lgdPos(4)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];
            xyMin(1)=max(xyMin(1),ax.XLim(1));
            xyMin(2)=max(xyMin(2),ax.YLim(1));
            xyMax(1)=min(xyMax(1),ax.XLim(2));
            xyMax(2)=min(xyMax(2),ax.YLim(2));
            xDiff=(xyMax(1)-xyMin(1));
            yDiff=(xyMax(2)-xyMin(2));
            % 重设位置属性
            ax.Legend.UserData.NewBkg.Position=[xyMin,xDiff,yDiff];
            ax.Legend.UserData.NewTitle.Position=[xyMin(1),xyMax(2)];
        end
    end
end